package com.asion.qkan.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.adapters.AllContentRecyAdapter;
import com.asion.qkan.entry.AllContentBean;
import com.asion.qkan.ui.contract.AllContentContract;
import com.asion.qkan.ui.model.AllContentModel;
import com.asion.qkan.ui.presenter.AllContentPresenter;
import com.asion.qkan.ui.view.ContentActivity;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HCJ on 2016/10/16.
 */

public class AllContentFragment extends BaseFragment<AllContentPresenter, AllContentModel> implements AllContentContract.View, AllContentContract.OnContentItemClickListener, View.OnClickListener {
    @BindView(R.id.all_content_order)
    TextView allContentOrder;
    @BindView(R.id.all_content_recy)
    RecyclerView allContentRecy;
    private AllContentRecyAdapter adapter;
    private String id;
    @Override
    protected int getLayoutResource() {
        return R.layout.allcontent_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
        mPresenter.setAllContentDatas(id);
    }

    @Override
    protected void initView() {
        Bundle arguments = getArguments();
        id = arguments.getString("id");
        adapter = new AllContentRecyAdapter(getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        allContentRecy.setLayoutManager(linearLayoutManager);
        allContentRecy.setAdapter(adapter);
        allContentOrder.setOnClickListener(this);
        adapter.setOnContentItemClickListener(this);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void showDataSuccess(AllContentBean allContentBean) {
        adapter.addAll(allContentBean.getData().getComics());
    }

    @Override
    public void showDataError(Throwable throwable) {
        throwable.printStackTrace();
    }

    @Override
    public void onContentItemClick(int position, AllContentBean.DataBean.ComicsBean comicsBean) {
        Bundle bundle = new Bundle();
        bundle.putString("subtitle",comicsBean.getTitle());
        Log.e("tag",comicsBean.getTitle());
        Log.e("tag",comicsBean.getUrl());
        bundle.putString("tag","all");
        bundle.putString("url",comicsBean.getUrl());
        ContentActivity.startAction(getActivity(),bundle);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.all_content_order:
                if(((TextView) view).getText().equals("倒序")){
                    ((TextView) view).setText("正序");
                }else {
                    ((TextView) view).setText("倒序");
                }
                adapter.reverse();
                break;
        }
    }
}
